package com.leased.service.impl;

import com.leased.exception.DatabaseException;
import com.leased.interceptor.PageMybatisInterceptor;
import com.leased.mapper.CUsersMapper;
import com.leased.mapper.manual.ICustomizedCUserMapper;
import com.leased.pojo.CUsers;
import com.leased.pojo.CUsersExample;
import com.leased.request.cuser.CUserListRequest;
import com.leased.response.CUserResponse;
import com.leased.response.MyCarsResponse;
import com.leased.service.CUserService;
import com.leased.utils.Page;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * Created by shencx on 2017/8/1.
 */
@Service
public class CUserServiceImpl implements CUserService{

    Logger LOG = LoggerFactory.getLogger(CUserServiceImpl.class);

    @Resource
    private CUsersMapper cUsersMapper;

    @Resource
    private ICustomizedCUserMapper customizedCUserMapper;

    /**
     * 添加车主信息
     * @param cUsers
     * @return
     * @throws DatabaseException
     */
    @Override
    public Integer addCUserInfo(CUsers cUsers) throws DatabaseException {
        try {
            if (cUsers == null){
                LOG.error("addCUserInfo 信息为空",cUsers);
                return Integer.valueOf(0);
            }
            cUsers.setRegisterTime(new Date());
            int val = cUsersMapper.insertSelective(cUsers);
            if (val > 0){
                return cUsers.getCuserId();
            }else {
                return Integer.valueOf(0);
            }
        } catch (Throwable e) {
            LOG.error("addCUserInfo 发生异常",e,cUsers);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 更新车主信息
     * @param cUsers
     * @return
     * @throws DatabaseException
     */
    @Override
    public boolean updateCUser(CUsers cUsers) throws DatabaseException {
        try {
            if (cUsers == null){
                LOG.error("updateCUser 信息为空",cUsers);
                return false;
            }
            Integer val = cUsersMapper.updateByPrimaryKeySelective(cUsers);
            return val>0?true:false;
        } catch (Throwable e) {
            LOG.error("updateCUser 发生异常",e,cUsers);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取车主信息列表
     * @param cUserListRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<CUserResponse> fetchCUserInfoPage(CUserListRequest cUserListRequest) throws DatabaseException {
        try {
            if (cUserListRequest == null){
                LOG.error("fetchCUserInfoPage 信息为空",cUserListRequest);
                return null;
            }
            PageMybatisInterceptor.startPage(cUserListRequest.getPageNum(),cUserListRequest.getPageSize());
            customizedCUserMapper.findCUserInfoList(cUserListRequest);
            Page<CUserResponse> page = PageMybatisInterceptor.endPage();
            return page;
        } catch (Throwable e) {
            LOG.error("fetchCUserInfoPage 发生异常",e,cUserListRequest);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取用户详情
     * @param cUserListRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public CUsers fetchCUserDetail(CUserListRequest cUserListRequest) throws DatabaseException {
        try {
            if (cUserListRequest == null){
                LOG.error("fetchCUserDetail 信息为空",cUserListRequest);
                return null;
            }
            CUsersExample example = new CUsersExample();
            CUsersExample.Criteria criteria = example.createCriteria();
            if (cUserListRequest.getcUserId() != null){
                criteria.andCuserIdEqualTo(cUserListRequest.getcUserId());
            }
            if (StringUtils.isNoneBlank(cUserListRequest.getUserName())){
                criteria.andUsernameEqualTo(cUserListRequest.getUserName());
            }
            if (StringUtils.isNoneBlank(cUserListRequest.getPhoneNumber())){
                criteria.andPhoneEqualTo(cUserListRequest.getPhoneNumber());
            }
            List<CUsers> list = cUsersMapper.selectByExample(example);
            if (list == null || list.size() == 0){
                return null;
            }else {
                return list.get(0);
            }
        } catch (Throwable e) {
            LOG.error("fetchCUserDetail 发生异常",e,cUserListRequest);
            throw new DatabaseException(e.getMessage());
        }
    }

    /**
     * 获取我的车队列表
     * @param cUserListRequest
     * @return
     * @throws DatabaseException
     */
    @Override
    public Page<MyCarsResponse> fetchMyFleetPage(CUserListRequest cUserListRequest) throws DatabaseException {
        try {
            if (cUserListRequest == null){
                LOG.error("fetchMyFleetPage 信息为空",cUserListRequest);
                return null;
            }
            PageMybatisInterceptor.startPage(cUserListRequest.getPageNum(),cUserListRequest.getPageSize());
            customizedCUserMapper.findMyFleetPage(cUserListRequest);
            Page<MyCarsResponse> page = PageMybatisInterceptor.endPage();
            return page;
        } catch (Throwable e) {
            LOG.error("fetchMyFleetPage 发生异常",e,cUserListRequest);
            throw new DatabaseException(e.getMessage());
        }
    }
}
